using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Core;
using System.Text.RegularExpressions;


namespace Yuanlin.Business
{
    public class CensorWordManager
    {
        private const string cacheKey = "CensorWord";
        public static string CensorWord(string formatted)
        {
            if (string.IsNullOrEmpty(formatted))
            {
                return formatted;
            }

            EntityList sensitiveKeywords = new EntityList();
            if (CacheManager.Get(cacheKey) == null)
            {
                sensitiveKeywords = BusinessManager.GetSensitiveKeywordList(RequestHelper.BuildGetSensitiveKeywordListRequest());
                CacheManager.Max(cacheKey, sensitiveKeywords);
            }
            else
            {
                sensitiveKeywords = CacheManager.Get(cacheKey) as EntityList;
            }

            RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Compiled;
            foreach (SensitiveKeyword sensitiveKeyword in sensitiveKeywords)
            {
                formatted = Regex.Replace(formatted, sensitiveKeyword.Word.Value, sensitiveKeyword.Replacement.Value, options);
            }

            return formatted;
        }
        public static void UpdateCensorWord(BaseUserControl source)
        {
            CacheManager.Remove(cacheKey);
            EntityList sensitiveKeywords = BusinessManager.GetSensitiveKeywordList(RequestHelper.BuildGetSensitiveKeywordListRequest());
            CacheManager.Max(cacheKey, sensitiveKeywords);
        }
    }
}
